package com.dearbinge.data.service.impl.transactionFlow;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.transactionFlow.TransactionFlowService;
import com.dearbinge.data.dao.TransactionFlowMapper;
import com.dearbinge.data.pojo.transactionFlow.TransactionFlow;
import com.dearbinge.utils.PageUtils;
import com.dearbinge.utils.date.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service(value = "transactionFlowService")
public class TransactionFlowServiceImpl implements TransactionFlowService {

    @Autowired
    private TransactionFlowMapper transactionFlowMapper;

    @Override
    public Integer insert(TransactionFlow transactionFlow) {
        Integer dataCode= transactionFlowMapper.insert(transactionFlow);
        return dataCode;
    }

    @Override
    public Integer update(TransactionFlow transactionFlow) {
        Integer dataCode= transactionFlowMapper.update(transactionFlow);
        return dataCode;
    }

    @Override
    public TransactionFlow getById(Integer id) {
        TransactionFlow transactionFlow= transactionFlowMapper.getById(id);
        return transactionFlow;
    }

    @Override
    public Integer delete(Integer id) {
        Integer dataCode = transactionFlowMapper.delete(id);
        return dataCode;
    }

    @Override
    public Page getByPage(String startTime, String endTime, String userId, Integer pageNo, Integer pageSize) {
        pageNo = PageUtils.getDefaultValues(pageNo, 1);
        pageSize = PageUtils.getDefaultValues(pageSize, 10);
        Page<Map<String,Object>> page = new Page<>(pageNo,pageSize);
        Integer count = transactionFlowMapper.count(userId,startTime,endTime);
        page.setTotal(count);
        List<Map<String,Object>> list = transactionFlowMapper.getByPage(userId,startTime,endTime,page);

        String str = null;
        String str2 = null;
        List<Map<String,Object>> mapList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        for (int index = 0;index < list.size();index++){
            Map<String, Object> map1 = list.get(index);
            List<Object> objectList = new ArrayList<>();
            String createTime = map1.get("create_time").toString();
            String[] s = createTime.split(" ");
            str = s[0];
            if (str2 != null && str2.equals(str)){
                str = null;
                map = new HashMap<>();
                continue;
            }
            for (int i = 0;i < list.size();i++){
                Map<String, Object> map2 = list.get(i);
                String createTime1 = map2.get("create_time").toString();
                String[] s1 = createTime1.split(" ");
                if (s[0].equals(s1[0])){
                    objectList.add(list.get(i));
                }
            }
            str2 = s[0];
            map.put("date",s[0]);
            map.put("arr",objectList);
            mapList.add(map);
        }

        page.setRecords(mapList);
        return page;
    }
}
